package com.wolfpire.section.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.profession.service.ProfessionService;
import com.wolfpire.section.service.SectionService;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.RoleService;

/**
 * @author xp
 *
 */

@Controller
@RequestMapping("/section")
public class SectionController {
	
	@Resource private SubjectService subjectService;
	@Resource private SectionService sectionService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("sectionManage/section_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<Section> queryList(EasyuiPage<Section> page, Section filterSection) {
		Page<Section> sections = this.sectionService.setPageDataList(page, filterSection);
		return new EasyuiPagination<Section>(sections);
	}
	 
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, @RequestParam(value = "chapterId", required = false) String chapterId) {
		ModelAndView mv = new ModelAndView("sectionManage/section_add");
		mv.getModelMap().put("chapterId", StringUtils.isBlank(chapterId) ? StringUtils.EMPTY : chapterId);
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("sectionManage/section_edit");
		if (null != id) {
//			User user = this.userService.get(id);
			Section section = this.sectionService.getCompleteSection(id);
			mv.getModelMap().put("section", section);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(Section newSection) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		Section targetSection = this.sectionService.getByName(newSection.getName());
		// 新增
		if (null == targetSection) {
			this.sectionService.saveOrUpdate(newSection);
			result = true;
			msg = "新增成功";
		} else {
		// 更新
			this.sectionService.saveOrUpdate(newSection);
			result = true;
			msg = "更新成功";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.sectionService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/sections")
	public List<Map<String,Object>> subjects(Section section) {
		/*if (null != profession && null != profession.getId()) {
			org.setParentId(org.getId());
			org.setId(null);
		}*/
//		List<Subject> subjects = this.subjectService.list(subject);
		List<Map<String, Object>> list = this.sectionService.bulidTree(section);
		return list;
	}
}
